import { BaseDeployPanel } from "./BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { DeployPanelItem } from "../DeployPanelItem";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { MsgManager } from "../../../../manager/MsgManager";
import { Cache } from "../../../../cache/Cache";
import { DataEvent } from "../../../../events/DataEvent";
import { EquipmentModuleConst } from "../../data/EquipmentModuleConst";
import { Alert } from "../../../../../../com/mui/controls/Alert";
import { NetDispatcher } from "../../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../../mvc/ServerCommand";
import { EquipUpdateParams } from "../../data/EquipUpdateParams";
import { EBind } from "../../../../../../Message/Public/EBind";
import { GlobalClass } from "../../../../../../com/mui/core/GlobalClass";
import { SPlayerItem } from "../../../../../../Message/Public/SPlayerItem";
import { ItemData } from "../../../../resource/info/ItemData";
import { ItemConfig } from "../../../../resource/ItemConfig";
import { JSON } from "../../../../../../com/mui/serialization/json/JSON";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
type int = number;
//class MakeHolePanel
    
    export  class MakeHolePanel extends BaseDeployPanel
    {
        constructor()
        {
            this.posData = [{"x": 157, "y": 110, "label": Language.getString(60222), "canRemove": true, "noBoarder": false}, {"x": 80, "y": 215, "label":"", "noBoarder": true, "isJewel": true}, {"x": 157, "y": 240, "label":"", "noBoarder": true, "isJewel": true}, {"x": 223, "y": 215, "label":"", "noBoarder": true, "isJewel": true}, {"x": 157, "y": 180, "label": Language.getString(60223), "canRemove": false, "noBoarder": true}];
            this.buttonLabel = Language.getString(60224);
            this._alarmText = Language.getString(60147);
            super();
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc2=null;
            var loc3=null;
            super.createChildren();
            var loc1=0;
            while (loc1 < this.posData.length) 
            {
                loc2 = this.posData[loc1];
                loc3 = new DeployPanelItem(loc1, loc2.noBoarder, loc2.isJewel);
                loc3.posIndex = loc1;
                loc3.x = loc2.x;
                loc3.y = loc2.y;
                loc3.label = loc2.label;
                if (loc2.canRemove) 
                {
                    loc3.mouseEnabled = true;
                    loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onItemDoubleClick);
                }
                addChild(loc3);
                allItem.push(loc3);
                ++loc1;
            }
            confirmButton.label = this.buttonLabel;
            confirmButton.x = 143;
            confirmButton.y = 297;
            confirmButton.textField.filters = [FilterConst.glowFilter];
            confirmButton.styleName = "ButtonSpecial";
            addChild(confirmButton);
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            this.updateHoles();
            return;
        }

        /* internal  */onItemDoubleClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            if (loc1.itemData) 
            {
                this.moveItemOutByIndex(loc1.posIndex);
            }
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            var e: Object /* flash.events.MouseEvent */=null;
            var closeAlert: Function;

            var loc1;
            e = arg1;
            if (allItem[0].itemData == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60148));
                return;
            }
            if (!allItem[4].itemData) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60225));
                return;
            }
            if (allItem[4].enabled == false) 
            {
                if (!Cache.instance.equipment.isAutoBuy) 
                {
                    MsgManager.showRollTipsMsg(Language.getStringByParam(60226, allItem[4].itemData.name));
                    return;
                }
            }
            if (!isEnoughMoney) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                MsgManager.showRollTipsMsg(Language.getString(60121));
                return;
            }
            if (this.checkIsChangeToBind()) 
            {
                closeAlert = (arg1: int): void
                {
                    if (arg1 == Alert.OK) 
                    {
                        onConfirm();
                    }
                    return;
                }
                Alert.show(this._alarmText, null, Alert.OK | Alert.CANCEL, null, closeAlert);
            }
            else 
            {
                this.onConfirm();
            }
            return;
        }

        /* internal  */onItemBuySuccess(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            var loc1=allItem[0].itemData ? [allItem[0].itemData] : [];
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, loc1, false, true));
            this.onButtonClick();
            return;
        }

        /* internal  */onConfirm(): void
        {
            confirmButton.enabled = false;
            var loc1=this.currentEquipmentData.uid;
            var loc2=this.currentEquipmentData.posType;
            var loc3=[];
            loc3.push(allItem[0].itemData.uid);
            if (allItem[4].itemData.uid != null) 
            {
                loc3.push(allItem[4].itemData.uid);
            }
            var loc4;
            (loc4 = new EquipUpdateParams()).operationId = this.operationId;
            loc4.autoUpdate = false;
            loc4.equipPosType = loc2;
            loc4.materials = loc3;
            loc4.autoBuyTone = Cache.instance.equipment.isAutoBuy;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc4, false, true));
            return;
        }

        /* internal  */checkIsChangeToBind(): Boolean
        {
            if (!(allItem[0].itemData.itemExInfo.bind == EBind._EBindYes) && allItem[4].itemData.itemInfo.item.bind == EBind._EBindYes) 
            {
                return true;
            }
            return false;
        }

        /* internal  */updateHoles(): void
        {
            var loc1=0;
            var loc3=0;
            var loc4=null;
            var loc5=null;
            var loc2=allItem[0].itemData;
            if (loc2) 
            {
                loc1 = 0;
                while (loc1 < 3) 
                {
                    loc3 = this["hole" + (loc1 + 1)];
                    if (loc3 == 0) 
                    {
                        allItem[loc1 + 1].itemData = null;
                        allItem[loc1 + 1].source = GlobalClass.getBitmap("NoHole");
                    }
                    else if (loc3 == 1) 
                    {
                        allItem[loc1 + 1].itemData = null;
                        allItem[loc1 + 1].source = null;
                    }
                    else 
                    {
                        (loc4 = new SPlayerItem()).itemCode = loc3;
                        loc5 = new ItemData(loc4);
                        allItem[loc1 + 1].itemData = loc5;
                    }
                    ++loc1;
                }
            }
            else 
            {
                loc1 = 0;
                while (loc1 < 3) 
                {
                    allItem[loc1 + 1].source = null;
                    ++loc1;
                }
            }
            return;
        }

        protected /* override */ updateStatus(): void
        {
            var loc1=true;
            var loc2=allItem[0].itemData;
            var loc3=allItem[4].itemData && allItem[4].enabled;
            if (!loc2 || !(this.hole1 == 0) && !(this.hole2 == 0) && !(this.hole3 == 0) || !loc3) 
            {
                loc1 = false;
            }
            confirmButton.enabled = true;
            return;
        }

        /* internal  */refreshUpdateConfigInfo(): void
        {
            var loc4=0;
            var loc5=null;
            var loc1=0;
            var loc2=allItem[0].itemData;
            var loc3=allItem[0].itemData;
            if (loc2) 
            {
                this.hole1 = loc3.itemExInfo.hole1;
                this.hole2 = loc3.itemExInfo.hole2;
                this.hole3 = loc3.itemExInfo.hole3;
                if (this.hole1 != 0) 
                {
                    if (this.hole2 != 0) 
                    {
                        if (this.hole3 == 0) 
                        {
                            loc4 = 3;
                        }
                    }
                    else 
                    {
                        loc4 = 2;
                    }
                }
                else 
                {
                    loc4 = 1;
                }
                loc5 = ItemConfig.instance.getUpdateInfo(ItemConfig.WeaponMakeHole, loc4)["condition"];
                this.updateConfigInfo = JSON.deserialize(loc5);
                this.updateConfigInfo.makeHoleLevel = loc4;
                loc1 = this.updateConfigInfo.cost;
                successRate = 100;
            }
            else 
            {
                this.updateConfigInfo = null;
                this.hole1 = 0;
                this.hole2 = 0;
                this.hole3 = 0;
                loc1 = 0;
                successRate = 0;
            }
            if (loc1 != _cost) 
            {
                _cost = loc1;
                dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            return;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            if (arg1.itemInfo.item.category != ECategory._ECategoryEquip) 
            {
                if (arg1.itemInfo.item.category != ECategory._ECategoryProp) 
                {
                };
            }
            else 
            {
                if (arg1.itemExInfo.hole3 != 0) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(60227));
                    return;
                }
                allItem[0].itemData = arg1;
                this.currentEquipmentData = arg1;
                this.refreshUpdateConfigInfo();
                this.updateHoles();
                this.updateStatus();
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [arg1], false, true));
            }
            return;
        }

        public moveItemOutByIndex(arg1: int): void
        {
            if (arg1 != 0) 
            {
                if (arg1 == 4) 
                {
                    allItem[4].itemData = null;
                    allItem[4].enabled = true;
                    this.updateStatus();
                }
            }
            else 
            {
                this.clear();
            }
            return;
        }

        public moveItemOutByUid(arg1: string): void
        {
            var loc1=allItem[0].itemData;
            var loc2=allItem[4].itemData;
            if (loc1.uid != arg1) 
            {
                if (loc2.uid == arg1) 
                {
                    allItem[4].itemData = null;
                    allItem[4].enabled = true;
                    this.updateStatus();
                }
            }
            else 
            {
                allItem[0].itemData = null;
                this.updateHoles();
                this.updateStatus();
            }
            return;
        }

        public /* override */ clear(): void
        {
            allItem[0].itemData = null;
            allItem[4].itemData = null;
            allItem[4].enabled = true;
            this.refreshUpdateConfigInfo();
            this.updateHoles();
            this.updateStatus();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            return;
        }

        public /* override */ setAutoChooseMaterial(arg1: Object): void
        {
            var loc1=arg1.material as Array<any>;
            allItem[4].itemData = loc1[0] as ItemData;
            allItem[4].enabled = arg1.isEnough as Boolean;
            this.updateStatus();
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            if (arg1 != EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60229));
                this.clear();
            }
            else 
            {
                this.clear();
                MsgManager.showRollTipsMsg(Language.getString(60228));
                addUpdateSuccessEffect("equipEffect_makeHole.swf", 176, 130);
            }
            this.putInItem(new ItemData(arg3));
            return;
        }

        public /* var */currentEquipmentData: ItemData;

        /* internal */ /* var */operationId: int=0;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */buttonLabel: string;

        /* internal */ /* var */hole1: int;

        /* internal */ /* var */hole2: int;

        /* internal */ /* var */hole3: int;

        /* internal */ /* var */updateConfigInfo: Object;

        /* internal */ /* var */_alarmText: string;
    }
